
package com.fjrcloud.utils;

/**
 * Author: Trun
 * Date: 2016-12-01
 * Description: LogUtils.java
 */

public class LogUtils {

    public static String customTagPrefix = "";

    private LogUtils() {
    }

    private static boolean printLog = true;

    private static String generateTag(StackTraceElement caller) {
        String tag = "%s.%s(L:%d)";
        String callerClazzName = caller.getClassName();
        callerClazzName = callerClazzName.substring(callerClazzName.lastIndexOf(".") + 1);
        tag = String.format(tag, callerClazzName, caller.getMethodName(), caller.getLineNumber());
        tag = (customTagPrefix == null || customTagPrefix == "") ? tag : customTagPrefix + ":" + tag;
        return tag;
    }

    private static StackTraceElement getCallerStackTraceElement() {
//        System.out.println("Thread.currentThread().getStackTrace()[0] = " + Thread.currentThread().getStackTrace()[0]);
//        System.out.println("Thread.currentThread().getStackTrace()[1] = " + Thread.currentThread().getStackTrace()[1]);
//        System.out.println("Thread.currentThread().getStackTrace()[2] = " + Thread.currentThread().getStackTrace()[2]);
//        System.out.println("Thread.currentThread().getStackTrace()[3] = " + Thread.currentThread().getStackTrace()[3]);
//        System.out.println("Thread.currentThread().getStackTrace()[4] = " + Thread.currentThread().getStackTrace()[4]);
        return Thread.currentThread().getStackTrace()[3];
    }

    public static void info(String content) {
        if (!printLog) return;
        StackTraceElement caller = getCallerStackTraceElement();
        String tag = generateTag(caller);

        System.out.println(tag + " = " + content);

    }

    ////////////////////////////////////////////


}
